//
//  QSAddVisitFirstCell.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/30.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSAddVisitFirstCell.h"
#import "BRPickerView.h"
#import "QSOtherSelectCompanyViewController.h"
@implementation QSAddVisitFirstCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configSubView];
    self.topGrayLine.backgroundColor = [UIColor QSColorGrayEAEAEA];
    self.centerGrayLine.backgroundColor = [UIColor QSColorGrayEAEAEA];
    self.bottomGrayLine.backgroundColor = [UIColor QSColorGrayEAEAEA];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)configSubView{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(kRealValue(0));
        make.trailing.equalTo(self.mas_trailing).offset(- kRealValue(0));
        make.top.equalTo(self.mas_top).offset(kRealValue(10));
        make.bottom.equalTo(self.mas_bottom).offset(kRealValue(0));
    }];
    [self.selectCompanyButton addTarget:self action:@selector(selectCompanyAction:) forControlEvents:UIControlEventTouchUpInside];
    self.nameTextField.delegate = self;
    self.nameTextField.textAlignment = NSTextAlignmentRight;
    [self configButton];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectDateAction:)];
    self.defaultDateLabel.userInteractionEnabled = YES;
    [self.defaultDateLabel addGestureRecognizer:tap];
}

- (void)configButton{
    self.kFaceToFaceButton.selected = NO;
    self.kFaceToFaceButton.layer.cornerRadius = kRealValue(4);
    self.kFaceToFaceButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    self.kFaceToFaceButton.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    self.kFaceToFaceButton.layer.borderWidth = 0.5;
    [self.kFaceToFaceButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    
    self.callButton.selected = NO;
    self.callButton.layer.cornerRadius = kRealValue(4);
    self.callButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    self.callButton.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    self.callButton.layer.borderWidth = 0.5;
    [self.callButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    
    self.netWorkCallButton.selected = NO;
    self.netWorkCallButton.layer.cornerRadius = kRealValue(4);
    self.netWorkCallButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    self.netWorkCallButton.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    self.netWorkCallButton.layer.borderWidth = 0.5;
    [self.netWorkCallButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
}
- (void)selectCompanyAction:(UIButton *)sender{
    [self.nameTextField resignFirstResponder];
//    if (self.companyArr.count) {
        QSOtherSelectCompanyViewController *vc = [QSOtherSelectCompanyViewController new];
        vc.didSelectCompanyBlock = ^(QSVisitInfoCompanyListModel *model) {
            [self.selectCompanyButton setTitle:model.companyName forState:UIControlStateNormal];
             //取出选中公司key赋值
            if (self.addVisitDidSelectCompanyBlock) {
                self.addVisitDidSelectCompanyBlock(model.companyKey);
            }
        };    
        [[UIViewController currentViewController].navigationController pushViewController:vc animated:YES];        
//    }else{
//        [QSAppKeyWindow showAutoHideHudWithText:@"请先绑定公司!"];
//    }
}

- (void)layoutSubviews{
    //只有一个企业默认选中
    if (_companyArr.count == 1) {
        QSVisitInfoCompanyListModel *model = self.companyArr.firstObject;
        [self.selectCompanyButton setTitle:model.companyName forState:UIControlStateNormal];
        if (self.addVisitDidSelectCompanyBlock) {
            self.addVisitDidSelectCompanyBlock(model.companyKey);
        }
    }
}
#pragma mark - UITextfield Delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.addVisitDidSelectContactBlock) {
        self.addVisitDidSelectContactBlock(textField.text);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (string.length == 0) return YES;//允许删除。
    if (textField.text.length >= 20) {
        return NO;
    }else{
        return YES;
    }
}

- (IBAction)faceButtonAction:(UIButton *)sender {
    sender.layer.borderColor = [UIColor colorWithHexString:@"#128BEC"].CGColor;
    [sender setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
    
    _callButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_callButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    _netWorkCallButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_netWorkCallButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    [self clickWithButton:sender];
}

- (IBAction)callButtonAction:(UIButton *)sender {
    sender.layer.borderColor = [UIColor colorWithHexString:@"#128BEC"].CGColor;
    [sender setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
    
    _kFaceToFaceButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_kFaceToFaceButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    _netWorkCallButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_netWorkCallButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    [self clickWithButton:sender];
}

- (IBAction)netWorkButtonAcion:(UIButton *)sender {
    sender.layer.borderColor = [UIColor colorWithHexString:@"#128BEC"].CGColor;
    [sender setTitleColor:[UIColor colorWithHexString:@"#128BEC"] forState:UIControlStateNormal];
    
    _callButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_callButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    _kFaceToFaceButton.layer.borderColor = [UIColor QSColorGrayCCCCCC].CGColor;
    [_kFaceToFaceButton setTitleColor:[UIColor QSColorBlack666666] forState:UIControlStateNormal];
    
    [self clickWithButton:sender];
    
}

- (void)clickWithButton:(UIButton *)sender{
    if (_addVisitDidSelectVistTypeBlock) {
        self.addVisitDidSelectVistTypeBlock(sender.currentTitle);
    }
}

- (void)selectDateAction:(UITapGestureRecognizer *)sender{
    [self.nameTextField resignFirstResponder];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RECYCLING_KEYBOARD" object:nil];
    [BRDatePickerView showDatePickerWithTitle:@"请选择走访时间" dateType:BRDatePickerModeHM defaultSelValue:nil resultBlock:^(NSDate *selectDate, NSString *selectValue) {
        NSTimeInterval stamp = [selectDate timeIntervalSince1970];
        NSString *selectTime = [NSString stringWithFormat:@"%ld", (long)stamp *1000];
        NSString *curruntDate = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] *1000];
        NSString *dateStr = [NSDate dateWithTimestamp:curruntDate format:@"yyyy-MM-dd"];
        self.defaultDateLabel.text = [NSString stringWithFormat:@"%@ %@", dateStr, selectValue];
        if (self.addVisitDidSelectDateBlock) {
            self.addVisitDidSelectDateBlock(selectTime);
        }
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    for (UIView *view in self.contentView.subviews) {
        [view endEditing:YES];
    }
    [self endEditing:YES];
}

/**
*  键盘即将显示的时候调用
*/
- (void)keyboardWillShow:(NSNotification *)note{
    [[UIViewController currentViewController] keyboardWillShow:note TextY:self.nameTextField.y];
}

/**
 *  键盘即将退出的时候调用
 */
- (void)keyboardWillHide:(NSNotification *)note{
    [[UIViewController currentViewController] keyboardHiden:note];
}
- (void)dealloc{
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
